/*
 * GroupView.java
 *
 * Created on August 5, 2006, 9:29 PM
 */
package com.floreantpos.ui.views.order;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

import com.floreantpos.PosException;
import com.floreantpos.main.Application;
import com.floreantpos.model.MenuGroup;
import com.floreantpos.model.MenuItem;
import com.floreantpos.model.dao.MenuItemDAO;
import com.floreantpos.swing.PosButton;
import com.floreantpos.ui.views.order.actions.ItemSelectionListener;
import java.util.Collections;
import java.util.Comparator;

/**
 *
 * @author MShahriar
 */
public class MenuItemView extends SelectionView {

  public final static String VIEW_NAME = "ITEM_VIEW";

  private Vector<ItemSelectionListener> listenerList = new Vector<ItemSelectionListener>();

  private MenuGroup menuGroup;

  /**
   * Creates new form GroupView
   */
  public MenuItemView() {
    super(com.floreantpos.POSConstants.ITEMS);
  }

  public MenuGroup getMenuGroup() {
    return menuGroup;
  }

  public void setMenuGroup(MenuGroup menuGroup) {
    this.menuGroup = menuGroup;

    reset();

    if (menuGroup == null) {
      return;
    }

    MenuItemDAO dao = new MenuItemDAO();
    try {
      List<MenuItem> items = dao.findByParent(menuGroup, false);

      Collections.sort(items, new Comparator() {
        public int compare(Object o1, Object o2) {
          MenuItem c1 = (MenuItem) o1;
          MenuItem c2 = (MenuItem) o2;
          return c1.getName().compareTo(c2.getName());
        }
      });

      int itemsQty = 0;

      if (items.size() > 3 && Application.isDemo()) {
        itemsQty = 3;
      } else {
        itemsQty = items.size();
      }

      for (int i = 0; i < itemsQty; i++) {
        MenuItem menuItem = items.get(i);
        ItemButton itemButton = new ItemButton(menuItem);
        addButton(itemButton);
      }
      revalidate();
      repaint();
    } catch (PosException e) {
      e.printStackTrace();
    }
  }

  public void addItemSelectionListener(ItemSelectionListener listener) {
    listenerList.add(listener);
  }

  public void removeItemSelectionListener(ItemSelectionListener listener) {
    listenerList.remove(listener);
  }

  private void fireItemSelected(MenuItem foodItem) {
    for (ItemSelectionListener listener : listenerList) {
      listener.itemSelected(foodItem);
    }
  }

  private void fireBackFromItemSelected() {
    for (ItemSelectionListener listener : listenerList) {
      listener.itemSelectionFinished(menuGroup);
    }
  }

  private class ItemButton extends PosButton implements ActionListener {

    MenuItem foodItem;

    ItemButton(MenuItem foodItem) {
      this.foodItem = foodItem;
      if (foodItem.getPrice() > 0) {
        setText(foodItem.getName() + " - " + Application.formatNumber(foodItem.getPrice()));
      } else {
        setText(foodItem.getName());
      }
      addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
      fireItemSelected(foodItem);
    }
  }

  @Override
  public void doGoBack() {
    fireBackFromItemSelected();
  }
}
